
//
//  RNTYYLabel.m
//  RNCommunity
//
//  Created by 朱吉峥 on 2020/2/29.
//  Copyright © 2020 朱吉峥. All rights reserved.
//

#import "RNTYYLabel.h"
#import "RNTYYLabelManager.h"

@interface RNTYYLabel ()

@property (nonatomic, strong) NSArray *emojiArr;
@property (nonatomic, strong) NSArray *emojiNameArr;

@end

@implementation RNTYYLabel

- (NSArray *)emojiArr
{
    if (!_emojiArr) {
        _emojiArr = [RNTYYLabelManager emojiArr];
    }
    return _emojiArr;
}

- (NSArray *)emojiNameArr
{
    if (!_emojiNameArr) {
        _emojiNameArr = [RNTYYLabelManager emojiNameArr];
    }
    return _emojiNameArr;
}

- (void)setFrameStr:(NSString *)frameStr
{
    _frameStr = frameStr;
    CGRect frame = CGRectFromString(frameStr);
    self.frame = frame;
    self.content = self.content;
}

- (void)setContentFontSize:(CGFloat)contentFontSize
{
    _contentFontSize = contentFontSize;
    self.content = self.content;
}

- (void)setGifFontSize:(CGFloat)gifFontSize
{
    _gifFontSize = gifFontSize;
    self.content = self.content;
}

- (void)setPreferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth
{
    [super setPreferredMaxLayoutWidth:preferredMaxLayoutWidth];
    self.content = self.content;
}

- (void)setContent:(NSString *)content
{
    _content = content;
    if (self.gifFontSize == 0 || self.contentFontSize == 0 || !self.frameStr || !content || !self.preferredMaxLayoutWidth) {
        return;
    }
    NSMutableArray *locationArr = [NSMutableArray array];
    NSString *resultStr = [self handleStr:content locationArr:locationArr];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:resultStr];
    attri.font = [UIFont systemFontOfSize:self.contentFontSize];
    self.attributedText = attri;
    PYWeakSelf
    [locationArr enumerateObjectsUsingBlock:^(NSArray * arr, NSUInteger idx, BOOL * _Nonnull stop) {
        if (arr.count == 0) {
            return ;
        }
        NSString *emoji = self.emojiArr[idx];
        for (NSNumber *location in arr) {
            [RNTYYLabelManager addOperationWithBlock:^{
                YYImage *image = [YYImage imageNamed:emoji];
                dispatch_async_on_main_queue(^{
                    PYStrongSelf
                    YYAnimatedImageView *gifView = [[YYAnimatedImageView alloc] initWithImage:image];
                    gifView.frame = CGRectMake(0, 0, strongSelf.gifFontSize, strongSelf.gifFontSize);
                    NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:gifView contentMode:UIViewContentModeScaleAspectFit attachmentSize:gifView.frame.size alignToFont:[UIFont systemFontOfSize:strongSelf.contentFontSize] alignment:YYTextVerticalAlignmentCenter];
                    [attri replaceCharactersInRange:NSMakeRange(location.integerValue, 1) withAttributedString:attachText];
                    strongSelf.attributedText = attri;
                    CGSize maxSize = CGSizeMake(strongSelf.preferredMaxLayoutWidth, MAXFLOAT);
                    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
                    strongSelf.textLayout = layout;
                    strongSelf.height = layout.textBoundingSize.height;
                });
            }];
        }
    }];
}

- (NSString *)handleStr:(NSString *)str locationArr:(NSMutableArray *)locationArr
{
    if (![locationArr isKindOfClass:[NSMutableArray class]] || !str) {
        NSLog(@"处理数据异常");
        return nil;
    }
    NSString *resultStr = str;
    for (NSString *emojiName in self.emojiNameArr) {
        NSMutableArray *arr = [NSMutableArray array];
        resultStr = [self handleStr:resultStr emojiName:emojiName arr:arr];
        [locationArr addObject:arr];
    }
    return resultStr;
}

- (NSString *)handleStr:(NSString *)str emojiName:(NSString *)emojiName arr:(NSMutableArray *)arr
{
    if (![arr isKindOfClass:[NSMutableArray class]] || !emojiName || !str) {
        NSLog(@"处理数据异常");
        return nil;
    }
    NSString *resultStr = str;
    NSRange range = [str rangeOfString:emojiName];
    NSMutableString *replaceStr = [NSMutableString string];
    for (int i = 0; i < emojiName.length; i++) {
        [replaceStr appendString:@"\0"];
    }
    if (range.location != NSNotFound) {
        [arr addObject:@(range.location)];
        resultStr = [str stringByReplacingCharactersInRange:range withString:replaceStr];
        return [self handleStr:resultStr emojiName:emojiName arr:arr];
    }
    return resultStr;
}

@end
